# Maintainer: gym603 <gui_yuan_miao@163.com>

_pyname=Theano
_realname=theano
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.0.5
pkgrel=5
pkgdesc="Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/Theano/Theano"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-scipy"
         "${MINGW_PACKAGE_PREFIX}-python-six")
optdepends=("${MINGW_PACKAGE_PREFIX}-gcc: Support to execute optimized C-implementations (for both CPU and GPU)")
makedepends=("patch" "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(https://pypi.org/packages/source/${_pyname::1}/${_pyname}/${_pyname}-${pkgver}.tar.gz
        0001-cmodule-obtain-mingw-w64-python-library.patch
        0002-not-use-np.distutils.__config__.blas_opt_info.patch)
sha256sums=('6e9439dd53ba995fcae27bf20626074bfc2fff446899dc5c53cb28c1f9202e89'
            'fd4a66aac925abdd9316bb142784c97e63d42ef590dddddf5d1771e69ea189df'
            '532ed23eadd9623fb87c7c50e25f64d2b68fd24cb4b9f546be2a154685b5799a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -b -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_pyname}-${pkgver}"
  apply_patch_with_msg \
    0001-cmodule-obtain-mingw-w64-python-library.patch \
    0002-not-use-np.distutils.__config__.blas_opt_info.patch

  cd "${srcdir}"
  rm -rf "python-build-${MSYSTEM}" | true
  cp -r "${_pyname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py --quiet build
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py --quiet install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1 --skip-build

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -Dm644 doc/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
